#include "Config.h"
#include <iostream>
#include <fstream>

namespace WinApp
{
	Config::Config(const std::string& filename)
	{
		m_FileName = filename;
		if(((int)m_FileName.find("\\")) < 0)
		{
			GetCurrentDirectory(256,m_Buf);
			m_FileName.insert(0,"\\");
			m_FileName.insert(0,m_Buf);
			std::ifstream tempfile(m_FileName.c_str());
			if(!tempfile)
			{
				throw std::exception("can't open config file.");
			}
			else
			{
				tempfile.close();
			}
		}
	}

	Config::~Config(void)
	{
	}

	int Config::GetInteger(const std::string& AppName, const std::string& KeyName, const int defaultValue)
	{
		GetPrivateProfileStringA(AppName.c_str(),KeyName.c_str(),NULL,m_Buf,256,m_FileName.c_str());
		return strlen(m_Buf) > 0 ? atoi(m_Buf) : defaultValue;
	}

	std::string Config::GetString(const std::string& AppName, const std::string& KeyName, const char* defaultValue)
	{
		GetPrivateProfileStringA(AppName.c_str(),KeyName.c_str(),NULL,m_Buf,256,m_FileName.c_str());
		if(defaultValue)
		{
			return strlen(m_Buf) > 0 ? (std::string)m_Buf : (std::string)defaultValue;
		}
		else
		{
			return (std::string)m_Buf;
		}
	}
}